//
//  ONDayAHeadModel.m
//
//  Created by   on 6/4/14
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ONDayAHeadModel.h"
#import "ONDayAHeadObjects.h"


NSString *const kONDayAHeadModelObjects = @"objects";
NSString *const kONDayAHeadModelTotalObjects = @"totalObjects";
NSString *const kONDayAHeadModelHasMoreObjects = @"hasMoreObjects";


@interface ONDayAHeadModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ONDayAHeadModel

@synthesize objects = _objects;
@synthesize totalObjects = _totalObjects;
@synthesize hasMoreObjects = _hasMoreObjects;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
    NSObject *receivedONDayAHeadObjects = [dict objectForKey:kONDayAHeadModelObjects];
    NSMutableArray *parsedONDayAHeadObjects = [NSMutableArray array];
    if ([receivedONDayAHeadObjects isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedONDayAHeadObjects) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedONDayAHeadObjects addObject:[ONDayAHeadObjects modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedONDayAHeadObjects isKindOfClass:[NSDictionary class]]) {
       [parsedONDayAHeadObjects addObject:[ONDayAHeadObjects modelObjectWithDictionary:(NSDictionary *)receivedONDayAHeadObjects]];
    }

    self.objects = [NSArray arrayWithArray:parsedONDayAHeadObjects];
            self.totalObjects = [[self objectOrNilForKey:kONDayAHeadModelTotalObjects fromDictionary:dict] doubleValue];
            self.hasMoreObjects = [[self objectOrNilForKey:kONDayAHeadModelHasMoreObjects fromDictionary:dict] boolValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForObjects = [NSMutableArray array];
    for (NSObject *subArrayObject in self.objects) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForObjects addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForObjects addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForObjects] forKey:kONDayAHeadModelObjects];
    [mutableDict setValue:[NSNumber numberWithDouble:self.totalObjects] forKey:kONDayAHeadModelTotalObjects];
    [mutableDict setValue:[NSNumber numberWithBool:self.hasMoreObjects] forKey:kONDayAHeadModelHasMoreObjects];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.objects = [aDecoder decodeObjectForKey:kONDayAHeadModelObjects];
    self.totalObjects = [aDecoder decodeDoubleForKey:kONDayAHeadModelTotalObjects];
    self.hasMoreObjects = [aDecoder decodeBoolForKey:kONDayAHeadModelHasMoreObjects];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_objects forKey:kONDayAHeadModelObjects];
    [aCoder encodeDouble:_totalObjects forKey:kONDayAHeadModelTotalObjects];
    [aCoder encodeBool:_hasMoreObjects forKey:kONDayAHeadModelHasMoreObjects];
}

- (id)copyWithZone:(NSZone *)zone
{
    ONDayAHeadModel *copy = [[ONDayAHeadModel alloc] init];
    
    if (copy) {

        copy.objects = [self.objects copyWithZone:zone];
        copy.totalObjects = self.totalObjects;
        copy.hasMoreObjects = self.hasMoreObjects;
    }
    
    return copy;
}


@end
